<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <!--组织机构-->
      <el-col :span="4" :xs="24">
        <div class="head-container">
          <el-input v-model="deptName" placeholder="请输入部门名称" clearable size="small" prefix-icon="el-icon-search"
            style="margin-bottom: 20px" />
        </div>
        <div class="head-container">
          <el-tree :data="deptOptions" :props="defaultProps" :expand-on-click-node="false"
            :filter-node-method="filterNode" ref="tree" default-expand-all @node-click="handleNodeClick" />
        </div>
      </el-col>
      <el-form-item label="姓名" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入姓名" clearable size="small"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="联系电话" prop="phone">
        <el-input v-model="queryParams.phone" placeholder="请输入联系电话" clearable size="small"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="身份证号码" prop="card">
        <el-input v-model="queryParams.card" placeholder="请输入身份证号码" clearable size="small"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="身份证正面照片" prop="cardJustImg">
        <el-input v-model="queryParams.cardJustImg" placeholder="请输入身份证正面照片" clearable size="small"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="身份证反面照片" prop="cardBackImg">
        <el-input v-model="queryParams.cardBackImg" placeholder="请输入身份证反面照片" clearable size="small"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="人员类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择人员类型" clearable size="small">
          <el-option v-for="dict in typeList" :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue" />
        </el-select>
      </el-form-item>
      <el-form-item label="昵称" prop="nickName">
        <el-input v-model="queryParams.nickName" placeholder="请输入昵称" clearable size="small"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="登录密码" prop="password">
        <el-input v-model="queryParams.password" placeholder="请输入登录密码" clearable size="small"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="所属组织" prop="companyId">
        <el-input v-model="queryParams.companyId" placeholder="请输入所属组织" clearable size="small"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="性别" prop="gender">
        <el-select v-model="queryParams.gender" placeholder="请选择性别" clearable size="small">
          <el-option v-for="dict in sexList" :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue" />
        </el-select>
      </el-form-item>
      <el-form-item label="微信ID" prop="wechatId">
        <el-input v-model="queryParams.wechatId" placeholder="请输入微信ID" clearable size="small"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['basic:people:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['basic:people:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['basic:people:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" :loading="exportLoading"
          @click="handleExport" v-hasPermi="['basic:people:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="peopleList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="姓名" align="center" prop="name" />
      <el-table-column label="联系电话" align="center" prop="phone" />
      <el-table-column label="身份证号码" align="center" prop="card" />
      <el-table-column label="身份证正面照片" align="center" prop="cardJustImg" >
        <template slot-scope="scope">
          <el-image v-if="scope.row.cardJustImg" style="width: 50px; height: 50px" :src="viewImgUrl+'?id='+scope.row.cardJustImg"
            :preview-src-list="[viewImgUrl+'?id='+scope.row.cardJustImg]" />
          <span v-else>暂无图片</span>
        </template>
      </el-table-column>
      <el-table-column label="身份证反面照片" align="center" prop="cardBackImg" >
        <template slot-scope="scope">
          <el-image v-if="scope.row.cardBackImg" style="width: 50px; height: 50px" :src="viewImgUrl+'?id='+scope.row.cardBackImg"
            :preview-src-list="[viewImgUrl+'?id='+scope.row.cardBackImg]" />
          <span v-else>暂无图片</span>
        </template>
      </el-table-column>
      <el-table-column label="人员类型" align="center" prop="type" :formatter="typeFormat" />
      <el-table-column label="昵称" align="center" prop="nickName" />
      <el-table-column label="登录密码" align="center" prop="password" />
      <el-table-column label="所属组织" align="center" prop="companyName" />
      <el-table-column label="性别" align="center" prop="gender" :formatter="sexFormat" />
      <el-table-column label="备注" align="center" prop="remarks" />
      <el-table-column label="微信ID" align="center" prop="wechatId" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['basic:people:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['basic:people:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改人员信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row :gutter="10">
          <el-col :span="12">
            <el-form-item label="姓名" prop="name">
              <el-input v-model="form.name" placeholder="请输入姓名" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="联系电话" prop="phone">
              <el-input v-model="form.phone" placeholder="请输入联系电话" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="10">
          <el-col :span="12">
            <el-form-item label="身份证号码" prop="card">
              <el-input v-model="form.card" placeholder="请输入身份证号码" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="人员类型" prop="type">
              <el-select v-model="form.type" placeholder="请选择人员类型" style="width: 100%;">
                <el-option v-for="dict in typeList" :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="10">
          <el-col :span="12">
            <el-form-item label="昵称" prop="nickName">
              <el-input v-model="form.nickName" placeholder="请输入昵称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="登录密码" prop="password">
              <el-input v-model="form.password" placeholder="请输入登录密码" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="10">
          <el-col :span="12">
            <el-form-item label="所属组织" prop="companyId">
              <treeselect v-model="form.companyId" :options="deptOptions" :show-count="true" placeholder="请选择所属组织" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="性别" prop="gender">
              <el-select v-model="form.gender" placeholder="请选择性别" style="width: 100%;">
                <el-option v-for="dict in sexList" :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="10">
          <el-col :span="12">
             <el-form-item label="身份证正面照片" prop="cardJustImg">
                 <imageUpload v-model="form.cardJustImg" />
             </el-form-item>
          </el-col>
          <el-col :span="12">
              <el-form-item label="身份证反面照片" prop="cardBackImg">
                <imageUpload v-model="form.cardBackImg" />
              </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="备注" prop="remarks">
          <el-input v-model="form.remarks" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    listPeople,
    getPeople,
    delPeople,
    addPeople,
    updatePeople,
    exportPeople
  } from "@/api/basic/people";
  import {
    getToken
  } from "@/utils/auth";
  import {
    treeselect
  } from "@/api/system/dept";
  import Treeselect from "@riophae/vue-treeselect";
  import "@riophae/vue-treeselect/dist/vue-treeselect.css";
  import ImageUpload from '@/components/ImageUpload';

  export default {
    name: "People",
    components: {
      Treeselect,ImageUpload
    },
    data() {
      return {
        // 后台服务器地址
        viewImgUrl: process.env.VUE_APP_BASE_API + "/common/viewImg",
        // 遮罩层
        loading: true,
        // 导出遮罩层
        exportLoading: false,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: false,
        // 总条数
        total: 0,
        // 人员信息表格数据
        peopleList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          name: null,
          phone: null,
          card: null,
          cardJustImg: null,
          cardBackImg: null,
          type: null,
          nickName: null,
          password: null,
          companyId: null,
          gender: null,
          remarks: null,
          wechatId: null
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          name: [{
            required: true,
            message: "姓名不能为空",
            trigger: "blur"
          }],
          phone: [{
            required: true,
            message: "联系电话不能为空",
            trigger: "blur"
          }],
          card: [{
            required: true,
            message: "身份证号不能为空",
            trigger: "blur"
          }],
          type: [{
            required: true,
            message: "人员类型不能为空",
            trigger: "change"
          }],
          gender: [{
            required: true,
            message: "性别不能为空",
            trigger: "blur"
          }],
        },
        // 人员类型集合
        typeList: [],
        // 性别集合
        sexList: [],
        // 部门树选项
        deptOptions: undefined,
        // 部门名称
        deptName: undefined,
        defaultProps: {
          children: "children",
          label: "label"
        },
        // 用户导入参数
        upload: {
          // 是否显示弹出层（用户导入）
          open: false,
          // 弹出层标题（用户导入）
          title: "",
          // 是否禁用上传
          isUploading: false,
          // 是否更新已经存在的用户数据
          updateSupport: 0,
          // 设置上传的请求头部
          headers: {
            Authorization: "Bearer " + getToken()
          },
          // 上传的地址
          url: process.env.VUE_APP_BASE_API + "/system/user/importData"
        },
      };
    },
    created() {
      this.getList();
      this.getTreeselect();
      this.getDicts("basic_people_type").then(response => {
        this.typeList = response.data;
      });
      this.getDicts("sys_user_sex").then(response => {
        this.sexList = response.data;
      });
    },
    watch: {
      // 根据名称筛选部门树
      deptName(val) {
        this.$refs.tree.filter(val);
      }
    },
    methods: {
      /** 查询人员信息列表 */
      getList() {
        this.loading = true;
        listPeople(this.queryParams).then(response => {
          this.peopleList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      /** 查询部门下拉树结构 */
      getTreeselect() {
        treeselect().then(response => {
          this.deptOptions = response.data;
        });
      },
      // 筛选节点
      filterNode(value, data) {
        if (!value) return true;
        return data.label.indexOf(value) !== -1;
      },
      // 节点单击事件
      handleNodeClick(data) {
        this.queryParams.deptId = data.id;
        this.getList();
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          name: null,
          phone: null,
          card: null,
          cardJustImg: null,
          cardBackImg: null,
          type: null,
          nickName: null,
          password: null,
          companyId: null,
          gender: null,
          remarks: null,
          wechatId: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加人员信息";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const id = row.id || this.ids
        getPeople(id).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改人员信息";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != null) {
              updatePeople(this.form).then(response => {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addPeople(this.form).then(response => {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$confirm('是否确认删除人员信息编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delPeople(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
      },
      /** 导出按钮操作 */
      handleExport() {
        const queryParams = this.queryParams;
        this.$confirm('是否确认导出所有人员信息数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportPeople(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        })
      },
      // 字典翻译
      sexFormat(row, column) {
        return this.selectDictLabel(this.sexList, row.gender);
      },
      typeFormat(row, column) {
        return this.selectDictLabel(this.typeList, row.type);
      },
      /** 导入按钮操作 */
      handleImport() {
        this.upload.title = "证件上传";
        this.upload.open = true;
      },
      // 文件上传中处理
      handleFileUploadProgress(event, file, fileList) {
        this.upload.isUploading = true;
      },
      // 文件上传成功处理
      handleFileSuccess(response, file, fileList) {
        this.upload.open = false;
        this.upload.isUploading = false;
        this.$refs.upload.clearFiles();
        this.$alert(response.msg, "导入结果", {
          dangerouslyUseHTMLString: true
        });
        this.getList();
      },
      // 提交上传文件
      submitFileForm() {
        this.$refs.upload.submit();
      },
    }
  };
</script>
